package com.ljx.network;

import cn.hutool.core.net.URLEncodeUtil;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.json.JSONUtil;
import okhttp3.*;

import java.io.IOException;
import java.math.BigInteger;
import java.security.PublicKey;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-05-13  14:35
 */
public class HttpUtil {

    public static void doGet(String url){
//        RSA rsa = new RSA();
//        URLEncodeUtil.encode()
//        RSA rsa = new RSA(new BigInteger("10001", 16), null,  new BigInteger("94dd2a81675fb779e6b9f7103698634cd400f27a154afa67af6166a43fc26417222a79506d34cacc7641946abda1785b7acf9910ad6a0978c91ec84d40b71d2891379af19ffb333e7517e390bd26ac312fe940c340466b4a5d4af1d65c3b5944078f96a1a51a5a53e4bc302818b7c9f63c4a1b07bd7d874cef1c3d4b2f5eb787194dd2a8675fb779e6b9f7103698634cd400f27a154afa67af6166a43fc26417222a79506d34cacc7641946abda1785b7acf9910ad6a0978c91ec84d40b71d2891379af19ffb333e7517e390bd26ac312fe940c340466b4a5d4af1d65c3b5944078f96a1a51a5a53e4bc302818b7c9f63c4a1b07bd7d874cef1c3d4b2f5eb7871", 16));
//        System.out.println(rsa.getPublicKeyBase64()); // 打印公钥的Base64编码


        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        try {
            Response response = client.newCall(request).execute();
            ResponseBody responseBody = response.body();

            if (response.isSuccessful() && responseBody != null) {
                String json = responseBody.string();

                System.out.println(json.toString());
            } else {
                System.out.println("请求失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void doPost(String url, RequestBody requestBody){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try {
            Response response = client.newCall(request).execute();
            ResponseBody responseBody = response.body();
            if (response.isSuccessful() && responseBody != null) {
                Result result = JSONUtil.toBean(responseBody.string(), Result.class);
                if (result.getResult().equals("success")){
                    System.out.println("连接成功");
                } else {
                    System.out.println(String.format("连接失败，原因:%s", result.getMessage()));
                }
            } else {
                System.out.println("请求失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
